package com.zy.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhy
 * @date 2025/6/19 17:08
 */
public class EncryptPlugin extends PluginAdapter {
    private String encryptFieldAnnotation = "@EncryptField";
    private String encryptFieldImportedType;
    private FullyQualifiedJavaType encryptFieldJavaType;
    private String responseNullAnnotation = "@JsonInclude(JsonInclude.Include.NON_NULL)";
    private String responseNullImportedType;
    private FullyQualifiedJavaType responseNullJavaType;
    private List<String> encryptFieldList = new ArrayList<>();

    private List<String> responseNullFieldList = new ArrayList<>();

    public List<String> getResponseNullFieldList() {
        return responseNullFieldList;
    }

    public EncryptPlugin() {
        encryptFieldImportedType = "com.zy.crypto.EncryptField";
        encryptFieldJavaType = new FullyQualifiedJavaType(encryptFieldImportedType);
        responseNullImportedType = "com.fasterxml.jackson.annotation.JsonInclude";
        responseNullJavaType = new FullyQualifiedJavaType(responseNullImportedType);
        encryptFieldList.add("mobilePhone");
        encryptFieldList.add("email");
        responseNullFieldList.add("password");
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        List<IntrospectedColumn> pkcs = introspectedTable.getPrimaryKeyColumns();
        if (("CHAR".equalsIgnoreCase(introspectedColumn.getJdbcTypeName()) || "VARCHAR".equalsIgnoreCase(introspectedColumn.getJdbcTypeName()) || "LONGVARCHAR".equalsIgnoreCase(introspectedColumn.getJdbcTypeName())) && pkcs.indexOf(introspectedColumn) < 0 && introspectedColumn.getLength() > 0) {
            if (encryptFieldList.stream().anyMatch(x -> x.equals(field.getName()))) {
                if (!topLevelClass.getImportedTypes().contains(encryptFieldJavaType)) {
                    topLevelClass.addImportedType(encryptFieldImportedType);
                }
                field.addAnnotation(encryptFieldAnnotation);
            }
            if (responseNullFieldList.stream().anyMatch(x -> x.equals(field.getName()))) {
                if (!topLevelClass.getImportedTypes().contains(responseNullJavaType)) {
                    topLevelClass.addImportedType(responseNullImportedType);
                }
                if (!topLevelClass.getAnnotations().contains(responseNullAnnotation)) {
                    topLevelClass.addAnnotation(responseNullAnnotation);
                }
            }
        }
        return true;
    }

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }
}
